<?php

class Domain_Vote {

    function getlist($item) {
        $db = new Model_Vote_User();
        $where = [
            'item' => $item
        ];
        return $db->lists($where);
    }

    function getlistbyid($id) {
        $db = new Model_Vote_User();
        $where = [
            'item' => $id
        ];
        return $db->lists($where);
    }

    public function hasNameInItem($id, $item) {
        $db = new Model_Vote_User();
        return $db->hasNameInItem($id, $item);
    }

    public function hasName($NO) {
        $db = new Model_Vote_User();
        return $db->hasName($NO);
    }
    public function userinfo($id) {
        $db = new Model_Vote_User();
        return $db->info($id);
    }

    public function NamePlus($id, $item) {
        $db = new Model_Vote_User();
        return $db->plusPoll($id, $item);
    }

    public function add($data) {
        $db = new Model_Vote_User();


        $data = $db->add($data);
        if (!$data) return NULL;

        return $data;


    }

    public function userset($userid, $data) {
        $db = new Model_Vote_User();
        return $db->edit($data, $userid);

    }

    public function delete($id, $itemid) {
        $db = new Model_Vote_User();
        return $db->deleteByNameAndItem($id, $itemid);
    }


    public function historyUserByDay($openid, $day = null, $id = null) {
        $db = new Model_Vote_History();
        return $db->getTotalByDay($openid, $day, $id);
    }

    public function historyAddUser($openid, $id, $item) {
        $db = new Model_Vote_History();
        return $db->insert([
            'openid' => $openid,
            'userid' => $id,
            'item'   => $item,
            'ctime'  => DI()->helper->getTime(),
            'ip'     => DI()->helper->getClientIp(),
        ]);
    }

    public function itemAdd($item, $userLimit, $nomineeLimit, $startTime, $endTime) {
        $data = [
            "item"         => $item,
            "userLimit"    => $userLimit,
            "nomineeLimit" => $nomineeLimit,
            "startTime"    => $startTime,
            "endTime"      => $endTime,
        ];
        $db = new Model_Vote_Item();

        $data = $db->add($data);
        if (!$data) return NULL;
        return $data;

    }

    public function itemUpdate($item, $userLimit, $nomineeLimit, $startTime, $endTime, $id) {
        $data = [
            "id"           => $id,
            "item"         => $item,
            "userLimit"    => $userLimit,
            "nomineeLimit" => $nomineeLimit,
            "startTime"    => $startTime,
            "endTime"      => $endTime,
        ];
        $db = new Model_Vote_Item();

        $isupdated = $db->set($item, $userLimit, $nomineeLimit, $startTime, $endTime, $id);
        if (!$isupdated) return NULL;
        return $data;
    }

    public function itemGet($id) {
        $db = new Model_Vote_Item();
        return $db->itemInfo($id);
    }

    public function itemDelete($id) {
        $db = new Model_Vote_Item();
        return $db->itemDelete($id);
    }

    public function itemList() {
        $db = new Model_Vote_Item();
        return $db->itemlist();
    }


}
